#!/bin/bash
#Script for creating junior and high leagues from upsolving
#@Version 103
#Parameters
#Contest id without last sumbol
cId="01316"
#Day number problem suffix
daySfx="1"
#Core contests path
path="/__saintdata"
#League letters
hLetters="A B C D E F G H I J"
jLetters="I J K L M N O P Q R"

#constants
#league suffixes
hSfx="1"
jSfx="2"
uSfx="3"

#variables initializing
#not suffixed path
nspath=$path/$cId

#contests folders creation
mkdir $nspath$jSfx
mkdir $nspath$hSfx

#link creation and files copying for junior league
mkdir $nspath$jSfx/conf
cp -r $nspath$uSfx/conf/* $nspath$jSfx/conf

sedstr="s/contest_id = ${cId:1}$uSfx/contest_id = ${cId:1}$jSfx/g"
sed "$sedstr" $nspath$jSfx/conf/serve.cfg >$nspath$jSfx/conf/serve.cfg.new
mv $nspath$jSfx/conf/serve.cfg.new $nspath$jSfx/conf/serve.cfg

mkdir $nspath$jSfx/var
cp -r $nspath$uSfx/var/* $nspath$jSfx/var
mkdir $nspath$jSfx/checkers
mkdir $nspath$jSfx/checkers/src
mkdir $nspath$jSfx/statements
mkdir $nspath$jSfx/tests
for i in $jLetters
do
	ln -s $nspath$uSfx/checkers/check_$i$daySfx $nspath$jSfx/checkers/check_$i
	ln -s $nspath$uSfx/checkers/src/check_$i$daySfx.cpp $nspath$jSfx/checkers/src/check_$i.cpp
	#we dont need to copy solutions here
	ln -s $nspath$uSfx/statements/$i$daySfx.xml $nspath$jSfx/statements/$i.xml
	ln -s $nspath$uSfx/tests/$i$daySfx $nspath$jSfx/tests/$i
done

#link creation and files copying for high league
mkdir $nspath$hSfx/conf
cp -r $nspath$uSfx/conf/* $nspath$hSfx/conf

sedstr="s/contest_id = ${cId:1}$uSfx/contest_id = ${cId:1}$hSfx/g"
sed "$sedstr" $nspath$hSfx/conf/serve.cfg >$nspath$hSfx/conf/serve.cfg.new
mv $nspath$hSfx/conf/serve.cfg.new $nspath$hSfx/conf/serve.cfg

mkdir $nspath$hSfx/var
cp -r $nspath$uSfx/var/* $nspath$hSfx/var
mkdir $nspath$hSfx/checkers
mkdir $nspath$hSfx/checkers/src
mkdir $nspath$hSfx/statements
mkdir $nspath$hSfx/tests
for i in $hLetters
do
        ln -s $nspath$uSfx/checkers/check_$i$daySfx $nspath$hSfx/checkers/check_$i
	ln -s $nspath$uSfx/checkers/src/check_$i$daySfx.cpp $nspath$hSfx/checkers/src/check_$i.cpp
        #we dont need to copy solutions here
        ln -s $nspath$uSfx/statements/$i$daySfx.xml $nspath$hSfx/statements/$i.xml
	ln -s $nspath$uSfx/tests/$i$daySfx $nspath$hSfx/tests/$i
done

#copying contest xml files
cp $path/data/contests/$cId$uSfx.xml $path/data/contests/$cId$hSfx.xml
sedstr="s/contest id=\"${cId:1}$uSfx\"/contest id=\"${cId:1}$hSfx\"/g"
sed "$sedstr" $path/data/contests/$cId$hSfx.xml >$path/data/contests/$cId$hSfx.xml.new
mv $path/data/contests/$cId$hSfx.xml.new $path/data/contests/$cId$hSfx.xml

sedstr="s/<name>(.{8})_upsolving/<name>\1_high/g"
sed -E "$sedstr" $path/data/contests/$cId$hSfx.xml >$path/data/contests/$cId$hSfx.xml.new
mv $path/data/contests/$cId$hSfx.xml.new $path/data/contests/$cId$hSfx.xml

sedstr="s/<name_en>(.{8})_upsolving/<name_en>\1_high/g"
sed -E "$sedstr" $path/data/contests/$cId$hSfx.xml >$path/data/contests/$cId$hSfx.xml.new
mv $path/data/contests/$cId$hSfx.xml.new $path/data/contests/$cId$hSfx.xml


cp $path/data/contests/$cId$uSfx.xml $path/data/contests/$cId$jSfx.xml
sedstr="s/contest id=\"${cId:1}$uSfx\"/contest id=\"${cId:1}$jSfx\"/g"
sed "$sedstr" $path/data/contests/$cId$jSfx.xml >$path/data/contests/$cId$jSfx.xml.new
mv $path/data/contests/$cId$jSfx.xml.new $path/data/contests/$cId$jSfx.xml

sedstr="s/<name>(.{8})_upsolving/<name>\1_junior/g"
sed -E "$sedstr" $path/data/contests/$cId$jSfx.xml >$path/data/contests/$cId$jSfx.xml.new
mv $path/data/contests/$cId$jSfx.xml.new $path/data/contests/$cId$jSfx.xml

sedstr="s/<name_en>(.{8})_upsolving/<name_en>\1_junior/g"
sed -E "$sedstr" $path/data/contests/$cId$jSfx.xml >$path/data/contests/$cId$jSfx.xml.new
mv $path/data/contests/$cId$jSfx.xml.new $path/data/contests/$cId$jSfx.xml

#Chmod 777
chmod -R 777 $path/data
chmod -R 777 $cpath$hSfx
chmod -R 777 $cpath$jSfx
